from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("week1/PyTorch学习/3-transformer/logs2")

img = Image.open("week1/PyTorch学习/dataset/train/ants/0013035.jpg")

# 使用 transforms.ToTensor() 将 PIL 图像转换为 Tensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor, 1)

# Normalize 图像(归一化)
trans_normalize = transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
img_normal = trans_normalize(img_tensor)
writer.add_image("Normalize", img_normal, 1)

# Resize 图像
trans_resize = transforms.Resize((512, 512))
img_resized = trans_resize(img)
# 再次转换为 Tensor
img_resized = trans_totensor(img_resized)
writer.add_image("Resize", img_resized, 1)

# Compose - resize - 2 (将两步操作合并)
trans_resize_2 = transforms.Resize((256, 256))
# PIL -> PIL -> Tensor
trans_compose = transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2 =trans_compose(img)
writer.add_image("Resize_2", img_resize_2, 1)

# RandomCrop 图像
trans_random_crop = transforms.RandomCrop((200, 200))
trans_compose_crop = transforms.Compose([trans_random_crop,trans_totensor])
for i in range(10):
    img_random_crop = trans_compose_crop(img)
    writer.add_image(f"RandomCrop_{i}", img_random_crop, i)

writer.close()

